package com.ppandroid.wifiap.util

import android.graphics.Bitmap
import android.graphics.Color
import android.util.Log
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.WriterException
import com.google.zxing.qrcode.QRCodeWriter
import java.util.*

/**
 * 二维码生成器
 */
object QrCodeGenerator {
    
    private const val TAG = "QrCodeGenerator"
    
    /**
     * 生成二维码
     * @param content 二维码内容
     * @param width 宽度
     * @param height 高度
     * @return 二维码Bitmap，失败返回null
     */
    fun generateQrCode(content: String, width: Int = 400, height: Int = 400): Bitmap? {
        return try {
            val hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java).apply {
                put(EncodeHintType.CHARACTER_SET, "UTF-8")
                put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.M)
                put(EncodeHintType.MARGIN, 1)
            }
            
            val qrCodeWriter = QRCodeWriter()
            val bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints)
            
            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            
            for (x in 0 until width) {
                for (y in 0 until height) {
                    bitmap.setPixel(x, y, if (bitMatrix[x, y]) Color.BLACK else Color.WHITE)
                }
            }
            
            bitmap
        } catch (e: WriterException) {
            Log.e(TAG, "生成二维码失败", e)
            null
        } catch (e: Exception) {
            Log.e(TAG, "生成二维码时发生未知错误", e)
            null
        }
    }
    
    /**
     * 生成带Logo的二维码
     * @param content 二维码内容
     * @param logo Logo图片
     * @param width 宽度
     * @param height 高度
     * @return 带Logo的二维码Bitmap，失败返回null
     */
    fun generateQrCodeWithLogo(
        content: String,
        logo: Bitmap?,
        width: Int = 400,
        height: Int = 400
    ): Bitmap? {
        val qrCode = generateQrCode(content, width, height) ?: return null
        
        if (logo == null) return qrCode
        
        return try {
            val logoSize = (width * 0.2f).toInt()
            val logoX = (width - logoSize) / 2
            val logoY = (height - logoSize) / 2
            
            // 缩放Logo
            val scaledLogo = Bitmap.createScaledBitmap(logo, logoSize, logoSize, true)
            
            // 创建新的Bitmap
            val result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            val canvas = android.graphics.Canvas(result)
            
            // 绘制二维码
            canvas.drawBitmap(qrCode, 0f, 0f, null)
            
            // 绘制Logo
            canvas.drawBitmap(scaledLogo, logoX.toFloat(), logoY.toFloat(), null)
            
            result
        } catch (e: Exception) {
            Log.e(TAG, "生成带Logo的二维码失败", e)
            qrCode
        }
    }
    
    /**
     * 生成WiFi配置二维码
     * @param ssid WiFi名称
     * @param password WiFi密码
     * @param encryptionType 加密类型 (WPA, WEP, nopass)
     * @param width 宽度
     * @param height 高度
     * @return WiFi配置二维码Bitmap，失败返回null
     */
    fun generateWifiQrCode(
        ssid: String,
        password: String,
        encryptionType: String = "WPA",
        width: Int = 400,
        height: Int = 400
    ): Bitmap? {
        val wifiConfig = "WIFI:T:$encryptionType;S:$ssid;P:$password;;"
        return generateQrCode(wifiConfig, width, height)
    }
}
